package com.mobilekites.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.mobilekites.R;
import com.mobilekites.api.model.OnDataChangedListener;

public abstract class PaginatedListAdapter extends BaseAdapter implements
		OnDataChangedListener {
	protected static enum FooterMode {
		ERROR, LOADING, NONE;
	}

	protected final Context mContext;
	private FooterMode mFooterMode;
	protected final LayoutInflater mLayoutInflater;
	protected OnClickListener mRetryClickListener;

	public PaginatedListAdapter(Context context, boolean isError,
			boolean isLoading) {
		mRetryClickListener = new OnClickListener() {

			public void onClick(View view) {
				if (mFooterMode == FooterMode.ERROR)
					retryLoadingItems();
				setFooterMode(FooterMode.LOADING);
			}

		};
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		if (!isError) {
			if (!isLoading)
				mFooterMode = FooterMode.NONE;
			else
				mFooterMode = FooterMode.LOADING;
		} else {
			mFooterMode = FooterMode.ERROR;
		}
	}

	private void setFooterMode(FooterMode footermode) {
		mFooterMode = footermode;
		notifyDataSetChanged();
	}

	protected View getErrorFooterView(View view, ViewGroup viewgroup) {
		ErrorFooterHolder mHolder = null;
		// must check it for case use this adapter with MultiItemRowListAdapter
		if (view == null || !(view.getTag() instanceof ErrorFooterHolder)) {
			mHolder = new ErrorFooterHolder();
			view = inflate(R.layout.kites_error_footer_layout, viewgroup, false);
			mHolder.button = ((Button) view
					.findViewById(R.id.kites_retry_button));
			mHolder.textView = (TextView) view
					.findViewById(R.id.kites_state_text);
			view.setTag(mHolder);
		} else {
			mHolder = (ErrorFooterHolder) view.getTag();
		}
		mHolder.textView.setText(getLastRequestError());
		mHolder.button.setOnClickListener(mRetryClickListener);
		return view;
	}

	protected FooterMode getFooterMode() {
		return mFooterMode;
	}

	public long getItemId(int i) {
		return (long) i;
	}

	protected abstract String getLastRequestError();

	protected View getLoadingFooterView(View view, ViewGroup viewgroup) {
		LoadingFooterHolder holder = null;
		// must check it for case use this adapter with MultiItemRowListAdapter
		if (view == null || !(view.getTag() instanceof LoadingFooterHolder)) {
			view = inflate(R.layout.kites_loading_footer_layout, viewgroup,
					false);
			holder = new LoadingFooterHolder();
			view.setTag(holder);
		} else {
			holder = (LoadingFooterHolder) view.getTag();
		}
		return view;
	}

	protected View inflate(int i, ViewGroup viewgroup, boolean attach) {
		return mLayoutInflater.inflate(i, viewgroup, attach);
	}

	public boolean isEnabled(int i) {
		return true;
	}

	protected abstract boolean isMoreDataAvailable();

	public void onDataChanged() {
		if (!isMoreDataAvailable())
			setFooterMode(FooterMode.NONE);
		else
			setFooterMode(FooterMode.LOADING);
	}

	protected abstract void retryLoadingItems();

	public void triggerFooterErrorMode() {
		setFooterMode(FooterMode.ERROR);
	}

	private static class ErrorFooterHolder {
		public Button button;
		public TextView textView;
	}

	private static class LoadingFooterHolder {

	}
}
